Skip to content

Conversation

@gguuss
Copy link
Contributor

@gguuss gguuss commented Jan 9, 2017

Adds snippet code for all 7 vision scenarios (GCS and local file) with the intention of longer-term sourcing all the snippets to the vision docs.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Jan 9, 2017
import io
import os

# Imports the Google Cloud client library
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment isn't necessary. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed extraneous comments.

# Loads the image into memory
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = vision_client.image(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line doesn't need to be within the with context.

image = vision_client.image(
content=content)

# Performs face detection on the image file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is obvious in context.

(Typically focus comments on why over what, especially if this isn't a quickstart)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed extraneous comments.

# Performs face detection on the image file
faces = image.detect_faces()

print 'Faces:'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use print-as-a-function (print(...)) so that this work with Python 3. :)


print 'Faces:'
for face in faces:
print 'anger', face.emotions.anger
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use .format over multiple args to print.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

print


def detect_faces_gcs(path):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid initialisms unless its the primary focus of the sample, prefer detect_faces_cloud_storage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

def detect_faces_gcs(path):
# Instantiates a client
vision_client = vision.Client()
image = vision_client.image(source_uri=path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this argument a path or is it a uri?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to URI where appropriate.

Copy link
Contributor

@theacodes theacodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there.

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This application demonstrates how to perform basic operations with the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: blank newline between license and docstring.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

anger=face.emotions.anger,
joy=face.emotions.joy,
surprise=face.emotions.surprise)
print('')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this strictly necessary or do you just like a blank newline at the end of the program output?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entirely aesthetic, I'm happy to remove the empty print statements.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

up to you.

print('Safe search:')
for safe in safe_searches:
print('adult: {adult}\nmedical: {medical}\nspoofed: {spoofed}\n' +
'violence: {violence}\n').format(adult=safe.adult,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh god, my eyes! No hanging indents, please:

print('oh god why did I make this string so long what is wrong with me.'.format(
    arg1=something, arg2=something, ...)

safe_searches = image.detect_safe_search()
print('Safe search:')
for safe in safe_searches:
print('adult: {adult}\nmedical: {medical}\nspoofed: {spoofed}\n' +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need newlines in your print, either make it separate print statements or use heredoc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I just removed the extraneous prints.

@theacodes
Copy link
Contributor

@dpebot merge when travis passes, pretty please.

@dpebot
Copy link
Collaborator

dpebot commented Jan 10, 2017

Okay! I'll merge when all statuses are green.

@dpebot dpebot added the automerge Merge the pull request once unit tests and other checks pass. label Jan 10, 2017
@theacodes
Copy link
Contributor

@gguuss oh no, the linter thinks your main is too complex! Might need to bump it the config in https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/nox.py#L141

@dpebot dpebot merged commit d732d44 into master Jan 11, 2017
@dpebot dpebot deleted the vision_cloud_client_snippets branch January 11, 2017 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge Merge the pull request once unit tests and other checks pass. cla: yes This human has signed the Contributor License Agreement.